﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;

namespace MS.INV.Prices
{
    public partial class L_Location : UserControl
    {
        public L_Location()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            if (!this.DesignMode)
            {
                DataTable Data = new DataTable();
                using (SysLst.L_ViTriBan l = new SysLst.L_ViTriBan())
                {
                    Data = l.LoadData();
                }
                foreach (DataRow row in Data.Rows)
                {
                    TreeNode tn = new TreeNode(Cvert.ToString(row["TenViTri"], ""));
                    tn.Tag = Cvert.ToInt32(row["IdViTri"], 0);
                    tree.Nodes.Add(tn);
                }
            }
        }

        public void SetData(DataTable Data)
        {
            if (Data == null || Data.Rows.Count <= 0) return;

            foreach (TreeNode node in tree.Nodes)
            {
                DataRow[] rS = Data.Select("IdViTri=" + Cvert.ToInt32(node.Tag, 0));
                if (!object.Equals(rS, null) && rS.Length > 0)
                    node.Checked = true;
            }
        }

        public DataTable GetChecked()
        {
            DataTable DT = new DataTable();
            DT.Columns.Add(new DataColumn("IdDoiTuong", Type.GetType("System.Int32")));
            foreach (TreeNode node in tree.Nodes )
            {
                if (node.Checked)
                {
                    DataRow row = DT.NewRow();
                    row["IdDoiTuong"] = Cvert.ToInt32(node.Tag, 0);
                    DT.Rows.Add(row);
                }
            }

            return DT;
        }
    }
}
